package cn.itcast.bos.action.base;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.base.StandardService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class StandardAction extends ActionSupport implements
		ModelDriven<Standard> {

	// 模型驱动

	private Standard standard = new Standard();
	//获取当前页和条数
	private int page;
	private int rows;
	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public Standard getStandard() {
		return standard;
	}

	public void setStandard(Standard standard) {
		this.standard = standard;
	}

	@Override
	public Standard getModel() {
		// TODO Auto-generated method stub
		return standard;
	}

	@Autowired
	private StandardService ss;

	// 保存收件标准的方法
	@Action(value = "standard_save", results = { @Result(name = "success", type = "redirect", location = "./pages/base/standard.html") })
	public String save() {
		ss.save(standard);
		return SUCCESS;

	}
	//分页查询的功能
	@Action(value="standard_pageQuery",results={@Result(name="success",type="json")})
	public String pageQuery(){
		//封装page,rows
		Pageable pageable = new PageRequest(page-1, rows);
		Page<Standard> pageData = ss.findPageData(pageable);
		//把total和rows返回到页面
		Map<Object,Object> result =  new HashMap<Object,Object>();
		result.put("total", pageData.getTotalElements());
		result.put("rows", pageData.getContent());
		//转换成json数据
		ActionContext.getContext().getValueStack().push(result);
		return SUCCESS;
	}
	@Action(value="standard_findAll",results={@Result(name="success",type="json")})
	public String findAll(){
		List<Standard> list = ss.findAll();
		//压入值栈转换成josn
		ActionContext.getContext().getValueStack().push(list);
		
		return SUCCESS;
		
		
	}
	
}
